﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<%@ Import  Namespace="Smartec.Accounts.Camel.AMS.Model.Facades" %>

<%
    var random = AMSManager.GenerateRandomInteger();
    var clientId = "picture" + random;

    Smartec.Accounts.Camel.AMS.Model.Entities.Picture picture = null;
    if(Model!=null)
        picture = PictureFacade.GetPicture(Model);
     %>

<div id="<%:clientId %>value">
   <%:Html.HiddenFor(x => x)%>
</div>
<div id="<%:clientId %>image">

    <img src="<%: PictureFacade.GetPictureUrl(Model, 100, true)%>" />
</div>
<%if(picture != null){ %>
    <span id="<%:clientId %>remove" class="t-button">Remove</span>
<% }
else
{%>
    <span id="<%:clientId %>remove" class="t-button" style="display:none;">Remove</span>
<% }%>
<br />
<input type="file" id="<%:clientId %>"/>
<script type="text/javascript">
    $(function () {
        $(function () {

            var authToken = '<%: this.Context.Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value%>';

            $("#<%:clientId %>").uploadify({
                "uploader": '<%: Url.Content("~/Content/flash/uploadify.swf")%>',
                "script": '<%:Url.Content("~/Picture/AsyncUpload") %>',
                "scriptData": { authToken: authToken },
                "multi": "false",
                "auto": true,
                "buttonText": "Upload",
                "cancelImg": '<%: Url.Content("~/Content/media/images/cancel-uploading.png")%>',
                "onComplete": function (event, ID, fileObj, response, data) {
                    var json = jQuery.parseJSON(response);
                    $('#<%:clientId %>image').html("<img src='" + json.imageUrl + "'/>");
                    $("#<%:clientId %>value input").val(json.pictureId);
                    $("#<%:clientId %>remove").show();
                }
            });

            $("#<%:clientId %>remove").click(function(e){
                $("#<%:clientId %>image").html("<img src='<%: PictureFacade.GetDefaultPictureUrl()%>'/>");
                $("#<%:clientId %>value input").val(0);
                $(this).hide();
            });

        });
    });
</script>
